package com.example.emergencyattempt01.activity.administrator_activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.emergencyattempt01.R;
import com.example.emergencyattempt01.activity.utils.AbstractNetWorkUtils;
import com.example.emergencyattempt01.activity.utils.NetWorkUtils;
import com.example.emergencyattempt01.activity.utils.Pack_DePackUtil;
import com.example.emergencyattempt01.activity.utils.ServerMessage;
import org.json.JSONException;

import java.io.IOException;

public class Add_User_Activity extends AppCompatActivity {

     EditText userNameEdit;
     EditText passwordEdit;
     EditText instituteEdit;
     EditText identityEdit;
     RadioGroup userTypeSelect;

     Button cancelEdit;
     Button confirmEdit;

     String userName;
     String password;
     String institute;
     String identity;
     String userType;

     String toServer;

     Handler handlerForAddingUser=new Handler()
     {
         @Override
         public void handleMessage(@NonNull Message msg) {
             switch (msg.what)
             {
                 case ADD_PERSON:
                     Toast.makeText(Add_User_Activity.this,"成功添加！",Toast.LENGTH_SHORT).show();
                     finish();
                     break;
                 case ADD_PERSON_FAIL:
                     Toast.makeText(Add_User_Activity.this,"网络错误！",Toast.LENGTH_SHORT).show();
                     break;
             }
         }
     };
     final int ADD_PERSON=0x010;
    final int ADD_PERSON_FAIL=0x011;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_user);
        // 初始化界面
        initPage();
        // 设置取消监听
        setCancel();
        // 设置发送逻辑
        setConfirm();

    }

    private void setConfirm() {
        confirmEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 收集信息
                collectMessage();
                // 打包数据
               packMessage();
               // 发送数据
                sendMessage();
            }
        });
    }

    private void sendMessage() {
        new Thread()
        {
            @Override
            public void run() {
                try {
                    ServerMessage serverMessage= NetWorkUtils.getInstance().create(toServer,"/add_new_user");
                    int code=serverMessage.getRespondCode();
                    if(code==200)
                    {
                        // 发送成功
                        Message message=new Message();
                        message.what=ADD_PERSON;
                        handlerForAddingUser.sendMessage(message);
                    }
                    else
                    {
                        Message message=new Message();
                        message.what=ADD_PERSON_FAIL;
                        handlerForAddingUser.sendMessage(message);
                    }
                }catch (JSONException e){e.printStackTrace();}
                catch (IOException e){e.printStackTrace();
                    Message message=new Message();
                    message.what=ADD_PERSON_FAIL;
                    handlerForAddingUser.sendMessage(message);}
            }
        }.start();
    }

    private void packMessage() {
        // 打包数据
            try {
                toServer= Pack_DePackUtil.packUserInfo(userName,password,institute,identity,userType);
            }catch (JSONException e){e.printStackTrace();}
    }

    private void collectMessage() {
        userName=userNameEdit.getText().toString().trim();
        password=passwordEdit.getText().toString().trim();
        institute=instituteEdit.getText().toString().trim();
        identity=identityEdit.getText().toString().trim();
        if(userTypeSelect.getCheckedRadioButtonId()==R.id.add_student)
        {
            userType="学生";
        }
        else if(userTypeSelect.getCheckedRadioButtonId()==R.id.add_administrator)
        {
            userType="管理员";
        }
    }

    private void setCancel() {
        cancelEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initPage() {
        userNameEdit=findViewById(R.id.add_user_username);
        passwordEdit=findViewById(R.id.add_user_password);
        instituteEdit=findViewById(R.id.add_user_institute);
        identityEdit=findViewById(R.id.add_user_identity);
        userTypeSelect=findViewById(R.id.add_user_userType);
        cancelEdit=findViewById(R.id.add_user_cancel);
        confirmEdit=findViewById(R.id.add_user_confirm);
    }
}